package com.wmz.picture.controller;

import com.wmz.picture.common.BaseResponse;
import com.wmz.picture.common.ResultUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author lihechun
 * @version 1.0
 * @description: 健康检查Controller
 * @date: 2024/12/13 22:05
 */
@RestController
@RequestMapping("/")
public class MainController {
    /**
     * 健康检查
     * @return
     */
    @GetMapping("/health")
    public BaseResponse<String> health() {
        return ResultUtils.success("程序检查正常！");
    }

    /*@GetMapping("/downloadZip")
    public void downloadZip(@RequestParam List<String> rowIds, HttpServletResponse response) throws IOException {
        // 创建临时目录
        Path tempDir = Files.createTempDirectory("temp_zip");
        int i = 0;
        try {
            // 将文件复制到临时目录
            for (String rowId : rowIds) {
                if(i++ == rowIds.size() - 1){
                    // 添加主体附件文件
                    copyFilesToTempDir(tempDir, rowId, "主体附件", new String[]{"5.pdf"});
                    // 添加中收附件文件
                    copyFilesToTempDir(tempDir, rowId, "中收附件", new String[]{"6.txt"});
                } else {
                    // 添加主体附件文件
                    copyFilesToTempDir(tempDir, rowId, "主体附件", new String[]{"1.txt", "2.xls"});
                    // 添加中收附件文件
                    copyFilesToTempDir(tempDir, rowId, "中收附件", new String[]{"3.ico", "4.docx"});
                }
            }

            // 设置响应头
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=test.zip");

            // 创建压缩输出流
            try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
                // 将临时目录中的文件添加到压缩文件中
                addFilesToZip(zipOut, tempDir);
            }
        } finally {
            // 删除临时目录及其内容
            deleteDirectory(tempDir);
        }
    }

    private void copyFilesToTempDir(Path tempDir, String rowId, String folder, String[] files) throws IOException {
        for (String file : files) {
            // 构建文件路径
            String filePath = "D:/test/" + file;
            File fileToCopy = new File(filePath);

            if (fileToCopy.exists()) {
                // 构建目标路径
                Path targetPath = tempDir.resolve(rowId).resolve(folder).resolve(file);
                Files.createDirectories(targetPath.getParent());
                Files.copy(fileToCopy.toPath(), targetPath);
            } else {
                System.out.println("File not found: " + filePath);
            }
        }
    }

    private void addFilesToZip(ZipOutputStream zipOut, Path tempDir) throws IOException {
        Files.walk(tempDir)
            .filter(path -> !Files.isDirectory(path))
            .forEach(path -> {
                ZipEntry zipEntry = new ZipEntry(tempDir.relativize(path).toString());
                try {
                    zipOut.putNextEntry(zipEntry);
                    Files.copy(path, zipOut);
                    zipOut.closeEntry();
                } catch (IOException e) {
                    System.err.println("Error adding file to zip: " + path);
                    e.printStackTrace();
                }
            });
    }

    private void deleteDirectory(Path directory) {
        try {
            Files.walk(directory)
                .sorted((p1, p2) -> -p1.compareTo(p2))
                .map(Path::toFile)
                .forEach(File::delete);
        } catch (IOException e) {
            System.err.println("Error deleting directory: " + directory);
            e.printStackTrace();
        }
    }*/
    /*@GetMapping("/downloadZip")
    public void downloadZip(@RequestParam List<String> rowIds, HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=test.zip");

        // 创建压缩输出流
        try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
            for (String rowId : rowIds) {
                // 添加主体附件文件
                addFilesToZip(zipOut, rowId, "主体附件", new String[]{"1.txt", "2.xls"});
                // 添加中收附件文件
                addFilesToZip(zipOut, rowId, "中收附件", new String[]{"3.ico", "4.docx"});
            }
        }
    }

    private void addFilesToZip(ZipOutputStream zipOut, String rowId, String folder, String[] files) throws IOException {
        for (String file : files) {
            // 构建文件路径
            String filePath = "D:/test/" + file;
            File fileToZip = new File(filePath);

            if (fileToZip.exists()) {
                // 创建一个ZipEntry
                ZipEntry zipEntry = new ZipEntry(rowId + "/" + folder + "/" + file);
                zipOut.putNextEntry(zipEntry);

                // 读取文件内容并写入压缩文件
                try (FileInputStream fis = new FileInputStream(fileToZip)) {
                    byte[] bytes = new byte[1024];
                    int length;
                    while ((length = fis.read(bytes)) >= 0) {
                        zipOut.write(bytes, 0, length);
                    }
                }
                zipOut.closeEntry();
            } else {
                System.out.println("File not found: " + filePath);
            }
        }
    }*/
}
